#include "dealcardcenterpanel.h"
#include <QDebug>
DealCardCenterPanel::DealCardCenterPanel(QWidget *parent,int make[],QVector<Poker *> & own_poker_v,QVector<Poker*> & other_poker_v,int *last_numer_v ,int *next_number_v ,size_t step,size_t step_m):
    GamePlayCenterPanel(parent,make,own_poker_v,other_poker_v,last_numer_v ,next_number_v),step_other(step),step_my(step_m)
{
    if(last_numer_v == nullptr || next_number_v == nullptr){
        qDebug() << "其他玩家手牌不知道";
        return ;
    }
    qDebug() << "开始发牌";
    qDebug() << step_other;
    qDebug() << step_my;
    //this->lower();
    out_all();
}

DealCardCenterPanel::~DealCardCenterPanel(){
    if(timer->isActive())
        timer->stop();
    delete timer;
}
//发其他人的牌
void DealCardCenterPanel::out_all(){
    timer = new QTimer(this);
    //计算自己牌的位置
    int own_mid_xx = (this->own_space.width() - (17*step_my+this->own_poker[0]->width()))/2;
    //qDebug() << "自己第一章牌：" << own_mid_xx;

    //qDebug() << "牌的长宽：" << this->own_poker[0]->height() << " " << this->own_poker[0]->width();
    qDebug() << "区域大小：" << this->own_space.height() << "," << this->own_space.width();
    int own_xx = this->own_space.x() + own_mid_xx;
    int own_mid_yy = (this->own_space.height() - this->own_poker[0]->height())/2;
    int own_yy = this->own_space.y() + own_mid_yy;

    qDebug() << "自己第一章牌：" << own_xx << "," << own_yy;

    //计算其他牌的中间位置
    int other_mid_xx = (this->next_space.width() - this->own_poker[0]->width())/2;
    int other_mid_yy = (this->next_space.height() - (17*step_other+this->other_poker[0]->height()))/2;

    //计算下家位置
    int next_xx = this->next_space.x() + other_mid_xx;
    int next_yy = this->next_space.y() + other_mid_yy;

    //计算上家位置
    int last_xx = this->last_space.x() + other_mid_xx;
    int last_yy = this->last_space.y() + other_mid_yy;
    connect(timer,&QTimer::timeout,[=](){
        static int count = 0;
        if(count < 54){
            switch (count % 3) {
                case 0://发自己
                    this->out_own(own_xx,own_yy,count/3);
                    break;
                case 1://发下家
                    this->out_next(next_xx,next_yy,count/3);
                    break;
                case 2://发上家
                    this->out_last(last_xx,last_yy,count/3);
                    break;
            }
            ++count;
            this->timer->start();
        }else{
            //发出信号
            emit this->state_end(play_state::DEAL_CARD);
        }

    });
    timer->start(100);
}

//发上家牌
void DealCardCenterPanel::out_last(int xx,int yy,int num){
    //out_other(this->getLast_space(), *(this->getLast_number()));
    Poker * poker = this->other_poker[num];
    //找到位置
    poker->move(xx,yy+num*(this->step_other));
    qDebug() << "第" << num << "张上家牌" ;
    //显示
    poker->raise();
    poker->show();
}

//发下家牌
void DealCardCenterPanel::out_next(int xx,int yy,int num){
    //out_other(this->getLast_space(), *(this->getLast_number()));
    Poker * poker = this->other_poker[num+18];
    //找到位置
    poker->move(xx,yy+num*step_other);
    qDebug() << "第" << num << "张下家牌";
    //显示
    poker->raise();
    poker->show();
}

//发自己
void DealCardCenterPanel::out_own(int xx,int yy,int num){
    Poker * poker = this->own_poker[num];
    //找到位置
    poker->move(xx+num*(this->step_my),yy);
    //显示
    qDebug() << "第" << num << "张自己牌";
    poker->raise();
    poker->show();
}

